package com.qh.resourcehub.login;

import cn.hutool.system.UserInfo;
import com.qh.resourcehub.sso.utils.CASUtil;
import com.qh.resourcehub.usersphere.entity.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/auth")
public class AuthController {


    @GetMapping("/validateTik")
    public ResponseEntity<User> validateTik(@RequestParam("ticket") String ticket, HttpServletRequest request) {
        // 通过 CAS 进行 ticket 校验
        // 获取当前登录的用户名
        String username = CASUtil.getAccountNameFromCas(request);
        Map<String, Object> userAttributesFromCas = CASUtil.getUserAttributesFromCas(request);
        System.out.println(userAttributesFromCas);
        if (username != null) {
            // 校验成功，返回用户信息
            User userInfo = new User(); // 设置其他用户信息
            userInfo.setName(username);
            return ResponseEntity.ok(userInfo);
        } else {
            // 如果未找到用户名，返回未授权响应
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(null);
        }
    }

}
